package com.ruoyi.web.controller.activity;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.framework.shiro.service.SysPasswordService;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysPostService;
import com.ruoyi.system.service.ISysRoleService;
import com.ruoyi.system.service.ISysUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.ativity.domain.ActStudent;
import com.ruoyi.ativity.service.IActStudentService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 学生信息Controller
 * 
 * @author YZD
 * @date 2022-07-10
 */
@Controller
@RequestMapping("/activity/student")
public class ActStudentController extends BaseController {
    private String prefix = "activity/student";

    @Autowired
    private IActStudentService actStudentService;

    @Autowired
    private ISysUserService userService;

    @Autowired
    private ISysRoleService roleService;

    @Autowired
    private ISysPostService postService;

    @Autowired
    private SysPasswordService passwordService;

    @Autowired
    private ISysDeptService deptService;

    @RequiresPermissions("activity:student:view")
    @GetMapping()
    public String student()
    {
        return prefix + "/student";
    }

    /**
     * 查询学生信息列表
     */
    @RequiresPermissions("activity:student:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ActStudent actStudent)
    {
        startPage();
        List<ActStudent> list = actStudentService.selectActStudentList(actStudent);
        for (ActStudent student : list) {
            student.setSysUser(userService.selectUserById(student.getSysUserId()));
        }
        return getDataTable(list);
    }

    /**
     * 导出学生信息列表
     */
    @RequiresPermissions("activity:student:export")
    @Log(title = "学生信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ActStudent actStudent) {
        List<ActStudent> list = actStudentService.selectActStudentList(actStudent);
        for (ActStudent student : list) {
            SysUser user = userService.selectUserById(student.getSysUserId());
            student.setSysUser(user);
            student.setSysDept(deptService.selectDeptById(user.getDeptId()));
        }
        ExcelUtil<ActStudent> util = new ExcelUtil<ActStudent>(ActStudent.class);
        return util.exportExcel(list, "学生信息数据");
    }

    /**
     * 新增学生信息
     */
    @GetMapping("/add")
    public String add(ModelMap mmap) {
        mmap.put("roles", roleService.selectRoleAll());
        mmap.put("posts", postService.selectPostAll());
        return prefix + "/add";
    }

    /**
     * 新增保存学生信息
     */
    @RequiresPermissions("activity:student:add")
    @Log(title = "学生信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ActStudent actStudent) {
        SysUser user = actStudent.getSysUser();
        if (UserConstants.USER_NAME_NOT_UNIQUE.equals(userService.checkLoginNameUnique(user.getLoginName()))) {
            return error("新增用户'" + user.getLoginName() + "'失败，登录账号已存在");
        } else if (UserConstants.USER_PHONE_NOT_UNIQUE.equals(userService.checkPhoneUnique(user))) {
            return error("新增用户'" + user.getLoginName() + "'失败，手机号码已存在");
        } else if (UserConstants.USER_EMAIL_NOT_UNIQUE.equals(userService.checkEmailUnique(user))) {
            return error("新增用户'" + user.getLoginName() + "'失败，邮箱账号已存在");
        }
        user.setSalt(ShiroUtils.randomSalt());
        user.setPassword(passwordService.encryptPassword(user.getLoginName(), user.getPassword(), user.getSalt()));
        //设置修改时间
        user.setCreateTime(new Date());
        user.setCreateBy(ShiroUtils.getLoginName());
        //把账号类型设置为教师类型
        user.setUserType("02");
        userService.insertUser(user);
        actStudent.setSysUserId(userService.selectOidById());
        return toAjax(actStudentService.insertActStudent(actStudent));
    }

    /**
     * 修改学生信息
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        ActStudent actStudent = actStudentService.selectActStudentById(id);
        actStudent.setSysUser(userService.selectUserById(actStudent.getSysUserId()));
        mmap.put("actStudent", actStudent);
        mmap.put("roles",roleService.selectRolesByUserId(actStudent.getSysUserId()));
        mmap.put("posts",postService.selectPostsByUserId(actStudent.getSysUserId()));
        return prefix + "/edit";
    }

    /**
     * 修改保存学生信息
     */
    @RequiresPermissions("activity:student:edit")
    @Log(title = "学生信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ActStudent actStudent) {
        SysUser user = actStudent.getSysUser();
        //校验用户是否允许操作
        userService.checkUserAllowed(user);
        if (UserConstants.USER_PHONE_NOT_UNIQUE.equals(userService.checkPhoneUnique(user))) {
            return error("修改用户'" + user.getLoginName() + "'失败，手机号码已存在");
        } else if (UserConstants.USER_EMAIL_NOT_UNIQUE.equals(userService.checkEmailUnique(user))) {
            return error("修改用户'" + user.getLoginName() + "'失败，邮箱账号已存在");
        }
        user.setUpdateTime(new Date());
        user.setUpdateBy(ShiroUtils.getLoginName());
        userService.updateUser(user);
        return toAjax(actStudentService.updateActStudent(actStudent));
    }

    /**
     * 删除学生信息
     */
    @RequiresPermissions("activity:student:remove")
    @Log(title = "学生信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        for (String tid : Convert.toStrArray(ids)){
            userService.deleteUserById(actStudentService.selectActStudentById(Long.valueOf(tid)).getSysUserId());
        }
        return toAjax(actStudentService.deleteActStudentByIds(ids));
    }
}
